﻿using Lightworks.Core.Constant;
using Lightworks.System.Dto;
using Lightworks.System.Models;
using Lightworks.System.Services;
using Microsoft.AspNetCore.Mvc;

namespace Lightworks.Api.Controllers
{
    [ApiController]
    [Route("api/system/role")]
    public class RoleController : ApiBaseController
    {
        ILogger<RoleController> logger;
        IRoleService roleService;
        public RoleController(ILogger<RoleController> logger, IRoleService roleService)
        {
            this.logger = logger;
            this.roleService = roleService;
        }

        [HttpGet("list")]
        public ApiResult List() 
        {
            var data = roleService.GetAll();
            return Ok(data);
        }

        [HttpGet("pageList")]
        public ApiResult PageList([FromQuery]SearchReq req) 
        {
            var data = roleService.GetRoleList(req);
            return Ok(data);
        }

        [HttpPost("delete/{id}")]
        public ApiResult Delete(long id) 
        {
            if (!roleService.Delete(id))
                return Error("删除失败");
            return Ok();
        }

        [HttpPost("save")]
        public ApiResult Save(Role role) 
        {
            if (!roleService.Save(role))
                return Error("保存失败");
            return Ok();
        }

        [HttpGet("getRes")]
        public ApiResult GetRes(long id) 
        {
            var data = roleService.GetResIdByRoleId(id);
            return Ok(data);
        }

        [HttpPost("setRes")]
        public ApiResult SetRes(UpdateRoleResReq req) 
        {
            if (!roleService.SetResourceForRole(req.RoleId, req.ResourceIds))
            {
                return Error("更新失败");
            }
            return Ok();
        }
    }
}
